package leetcode.doublepointer;

import java.util.Arrays;

//88. 合并两个有序数组
public class MergeArray {
    public static void main(String[] args) {
      int[] a =   new int[]{0,0,0};
      int[] b =   new int[]{1,2,3};
      merge2(a,0,b,3);
        System.out.println(Arrays.toString(a));
    }
    public static void merge(int[] nums1, int m, int[] nums2, int n) {

        int j =0;
        for (int i =m;i<m+n;i++){
            if (j<n ){
                nums1[i] = nums2[j++];
            }

        }
        Arrays.sort(nums1);

    }

    //双指针
    public static void merge2(int[] nums1, int m, int[] nums2, int n) {
        int p = m-1;
        int q = n-1;
        int index = nums1.length-1;
        while (q>=0){
            if (p<0 || nums2[q]>nums1[p]){
                nums1[index--] = nums2[q--];
            }else {
                nums1[index--] = nums1[p--];
            }
        }


    }

}
